//
//  UIColor+add.m
//  GK
//
//  Created by Seven on 2018/12/19.
//  Copyright © 2018 Seven. All rights reserved.
//

#import "UIColor+add.h"

UIColor *ColorWithString(NSString *hexString){
    return [UIColor colorWithHexString:hexString];
}

UIColor *ColorWithHex(NSInteger hex){
    return [UIColor colorWithHex:hex];
}

UIColor *ColorWithHexAplha(NSInteger hex, CGFloat alpha){
    return [UIColor colorWithHex:hex alpha:alpha];
}


UIColor *ColorRandom(void){
    int r = arc4random_uniform(255);
    int g = arc4random_uniform(255);
    int b = arc4random_uniform(255);
    
    CGFloat tmp = r*0.299 + g*0.578 + b*0.114 - 192;
    
    return RGBCOLOR(r, g, b);
}

@implementation UIColor(add)
+ (UIColor*) colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
    return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                           green:((float)((hexValue & 0xFF00) >> 8))/255.0
                            blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue];
}

+ (UIColor*) colorWithHex:(NSInteger)hexValue
{
    return [UIColor colorWithHex:hexValue alpha:1.0];
}

+ (NSString *) hexFromUIColor: (UIColor*) color {
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[0]
                                green:components[0]
                                 blue:components[0]
                                alpha:components[1]];
    }
    
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    
    return [NSString stringWithFormat:@"#%x%x%x", (int)((CGColorGetComponents(color.CGColor))[0]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[1]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];
}


+ (UIColor *)colorFromString:(NSString *)string{
    return [UIColor colorWithHexString:string];
}

+ (UIColor *)colorFromString:(NSString *)hexString opacity:(CGFloat)opacity
{
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; // bypass '#' character
    [scanner scanHexInt:&rgbValue];
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:opacity];
}

//颜色rgba渐变值
+ (UIColor *)gradientFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor progress:(CGFloat)progress{
    CGFloat fr, fg, fb, fa, tr, tg, tb, ta;
    [fromColor getRed:&fr green:&fg blue:&fb alpha:&fa];
    [toColor getRed:&tr green:&tg blue:&tb alpha:&ta];
    return _RGB(fr - (fr -tr)*progress, fg - (fg -tg)*progress, fb - (fb -tb)*progress, fa - (fa -ta)*progress);
}


- (BOOL)isDark{
    CGFloat fr, fg, fb ,fa;
    [self getRed:&fr green:&fg blue:&fb alpha:&fa];
    if (fr*0.299 + fg*0.578 + fb*0.114 >= 192) {
        return YES;
    }
    return NO;
}
@end
